using Moq;
using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class IdentifierKeywordProductionSpecification {
        [Test]
        public void ShouldParseIfIdentifierProductionReturnsIdentifierTermWithCorrectKeyword () {
            var context = new ParseContext();
            ParseResult result = ParseWithKeyword (context, "keyword", "keyword");

            Assert.That (result, Is.Not.Null);
            Assert.That (result.Index, Is.EqualTo (4));
            Assert.That (result.Term, Is.Null);
            Assert.That (result.Context, Is.EqualTo (context));
        }

        [Test]
        public void ShouldNotParseIfIdentifierProductionReturnsIdentifierTermWithoutCorrectKeyword () {
            var context = new ParseContext();
            ParseResult result = ParseWithKeyword (context, "not-same-keyword", "keyword");

            Assert.That (result, Is.Null);
        }

        private ParseResult ParseWithKeyword (ParseContext context, string actualKeyword, string expectedKeyword) {
            var id = new Mock<IProduction> ();
            var identifierTerm = new ParseResult (4, new IdentifierTerm (actualKeyword, null), context);
            id.Setup (
                i =>
                i.Parse (It.IsAny<char[]> (), It.IsAny<int> (), It.IsAny<ParseContext> (), It.IsAny<string> (),
                         It.IsAny<ParseEnvironment> ())).Returns (identifierTerm);

            var keywordProduction = new IdentifierKeywordProduction (expectedKeyword, id.Object);

            return keywordProduction.Parse ("stuff");
        }
    }
}